package com.bole.controller;

import com.alibaba.fastjson.JSONObject;
import com.bole.data.Result;
import com.bole.dto.permission.AdminDto;
import com.bole.dto.permission.MenuDto;
import com.bole.dto.permission.RoleDto;
import com.bole.feign.AdminFeign;
import com.bole.feign.ArticleTypeFeign;
import com.bole.feign.MenuFeign;
import com.bole.feign.RoleFeign;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author yakamaxi
 * @version 1.0
 * @date 2020/8/12 17:00
 * 用于跳转页面
 */
@Controller
public class IndexController {

    @Resource
    private MenuFeign menuFeign;
    @Resource
    private RoleFeign roleFeign;
    @Resource
    private AdminFeign adminFeign;

    @Resource
    private ArticleTypeFeign articleTypeFeign;
    @RequestMapping({"/", "/toLogin"})
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/index")
    public String toIndex() {
        return "index";
    }

    @RequestMapping("/welcome")
    public String toWelcome() {
        return "welcome";
    }

    @RequestMapping("/toRolePage")
    public String toRolePage() {
        return "admin-role";
    }

    @RequestMapping("/toMenuPage")
    public String toMenuPage() {
        return "admin-menu";
    }

    @RequestMapping("/menu/toAdd/{nodeId}")
    public String toAdd(@PathVariable("nodeId") Long nodeId, Model model) {
        model.addAttribute("nodeId", nodeId);
        return "menu/add";
    }

    @RequestMapping("/menu/toUpdate/{nodeId}")
    public String toMenuUpdate(@PathVariable("nodeId") Long nodeId, Model model) {
        Result menuById = menuFeign.getMenuById(nodeId);
        if (menuById.getCode().equals(200)) {
            model.addAttribute("menu", menuById.getData());
        }
        return "menu/update";
    }

    @RequestMapping("/role/toAdd")
    public String toRoleAdd() {
        return "role/add";
    }

    @RequestMapping("/role/toUpdate/{id}")
    public String toRoleUpdate(@PathVariable("id") Long id, Model model) {
        Result roleById = roleFeign.getRoleById(id);
        if (roleById.getCode().equals(200)) {
            model.addAttribute("role", roleById.getData());
        }
        return "role/update";
    }

    @RequestMapping("/toAdminPage")
    public String toAdminPage(Model model) {
        Result result = roleFeign.get();
        if (result.getCode().equals(200)) {
            model.addAttribute("roleList", result.getData());
        }
        return "admin-list";
    }

    @RequestMapping("/admin/toUpdate/{id}")
    public String toAdminUpdate(@PathVariable("id") Long id, Model model) {
        Result byId = adminFeign.getById(id);
        if (byId.getCode().equals(200)) {
            LinkedHashMap<String,Object> map = (LinkedHashMap<String, Object>) byId.getData();

            AdminDto adminDto = JSONObject.parseObject(JSONObject.toJSONString(map.get("admin")), AdminDto.class);

            List<RoleDto> roleList = (List<RoleDto>) map.get("roleList");
            Object rid = map.get("rid");
            model.addAttribute("admin", adminDto);
            model.addAttribute("rid",rid);
            model.addAttribute("roleList",roleList);
        }
        return "admin/update";
    }

    @RequestMapping("/toArticleClass")
    public String toArticleClass() {
        return "article-class";
    }
    @RequestMapping("/toArticleTypeUpdate/{id}")
    public String toArticleTypeUpdate(@PathVariable("id") Integer id,Model model) {
        Result result = articleTypeFeign.findArticleTypeById(id);
        model.addAttribute("articleType",result.getData());
        return "article-class-edit";
    }
}
